var util = require('../utils/util');
var daoUtil = require('../utils/daoUtil');
var stockPos = require('../class/stockPosition');

module.exports = {

    create: function (params, cb) {
        var validate = daoUtil.validate(['balance', 'price','stockGroupId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var stockPosition = new stockPos();
            stockPosition.init(params);
            models.stock_position.create(stockPosition.getParams(), function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    update: function (params, cb) {
        params.stockGroupId = params.stockGroupId.rowId;
        var validate = daoUtil.validate(['stockGroupId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var stockPosition = new stockPos();
            stockPosition.init(params);
            models.stock_position.update({stockGroupId: params.stockGroupId}, stockPosition.getParams()).exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    findByGroupId:function (params,cb) {
        var validate = daoUtil.validate(['stockGroupId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.stock_position.findOne(params).populate('stockGroupId').exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{

                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },

    deleteByIds:function (rowIds, cb) {
        daoUtil.proxy(function(models){
            models.stock_deal.destroy({rowId:rowIds}).exec(function (err, result) {
                if (err) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(result));
                }
            });
        },cb);
    }
};